//
//  RegisterFirstViewController.m
//  1243
//
//  Created by 我的 on 16/4/6.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "RegisterFirstViewController.h"
#import "RegisterSecondViewController.h"
#import "NetWorkTools.h"
#import "RegiseterTwoViewController.h"
@interface RegisterFirstViewController ()<UITextFieldDelegate>
/**
 *  手机号码
 */
@property (weak, nonatomic) IBOutlet UITextField *phoneNumText;
/**
 *  密码
 */
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
/**
 *  重复密码
 */
@property (weak, nonatomic) IBOutlet UITextField *repeatPasswordText;
/**
 *  姓名
 */
@property (weak, nonatomic) IBOutlet UITextField *nameText;
/**
 *  交易密码
 */
@property (weak, nonatomic) IBOutlet UITextField *dealPasswordText;
/**
 *  立即注册
 */
@property (weak, nonatomic) IBOutlet UIButton *registerButton;

@property (weak, nonatomic) IBOutlet UITextField *eMail;

@property (nonatomic, strong)NSDictionary *dic;

@property (weak, nonatomic) IBOutlet UITextField *shenFenZhengTextField;

@property (weak, nonatomic) IBOutlet UITextField *idcard;

@end

@implementation RegisterFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:44 / 255.0 green:87 / 255.0 blue:229 / 255.0 alpha:1];
     self.navigationController.navigationBar.translucent = NO;
    [self setupUI];
    
    self.dealPasswordText.delegate = self;
    self.dealPasswordText.tag = 100;
    
    self.nameText.delegate = self;
    self.nameText.tag = 101;
    
    self.eMail.delegate = self;
    self.eMail.tag = 100;
    
    self.dealPasswordText.delegate = self;
    self.dealPasswordText.tag = 101;
    
    self.shenFenZhengTextField.delegate = self;
    self.shenFenZhengTextField.tag = 100;
}

- (void)setupUI
{
    self.title = @"注册";
    
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(clickBtnBack)];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self.registerButton addTarget:self action:@selector(didClickRegisterButton) forControlEvents:UIControlEventTouchUpInside];

}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignFitstResponedTextField];
}


- (void)clickBtnBack {
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)resignFitstResponedTextField {
    [self.phoneNumText resignFirstResponder];
    [self.passwordText resignFirstResponder];
    [self.repeatPasswordText resignFirstResponder];
    [self.nameText resignFirstResponder];
    [self.dealPasswordText resignFirstResponder];
    [self.shenFenZhengTextField resignFirstResponder];
    [self.eMail resignFirstResponder];

}

- (void)clearPassWordTextField {
    self.passwordText.text = nil;
    self.repeatPasswordText.text = nil;
    self.dealPasswordText.text = nil;
}

- (IBAction)clickNextBtn:(UIButton *)sender {
//    
//    [self.navigationController pushViewController:[[RegisterSecondViewController alloc] init] animated:YES];
    
    
}

- (void)didClickRegisterButton {
    [self resignFitstResponedTextField];
    
    if (![self.passwordText.text isEqualToString: self.repeatPasswordText.text]) {
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次密码输入有误,请重新输入" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
        
        [self clearPassWordTextField];
        
        return;
    }
    
    if ([self.passwordText.text isEqualToString: self.dealPasswordText.text]) {
        if ([self.passwordText.text isEqualToString:@""]) {
            return;
        }
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"交易密码与登录密码重复,请重新设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
        [self clearPassWordTextField];
        
        return;
    }
    if (self.passwordText.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;

    }
    
    if (self.idcard.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写身份证" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    if (![self isMobileNumber:self.phoneNumText.text]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确格式的手机号" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alert show];
        return;
    }
    if (self.nameText.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写真实姓名" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    
    
   self.dic= @{
               
               //  VNUMBER  自己的
                           @"UserName":self.phoneNumText.text,
                           @"Password":self.repeatPasswordText.text,
                           @"Buy_Password":self.dealPasswordText.text,
                           @"RealName":self.nameText.text,
                           @"Email":@"",
//                           @"V_Number":@"1001",
//                           @"V_Name":@"小黑",
                           @"LastLoginTime":@"2016-04-12T15:47:20.6938427+08:00",
                           @"PNumber1":@"1002",
                           @"PName1":@"小兰",
                           @"PNumber2":@"1003",
                           @"PName2":@"小红;",
                           @"PNumber3":@"1004",
                           @"PName3":@"小白",
                           @"UserRole":@"VIP",
                           @"Money":@"0.0000",
                           @"fh_Money":@"0.00",
                           @"gq_Money":@"0.0000",
                           @"gwj_Money":@"0.00",
                           @"Dl_Money":@"0.0000",
                           @"Status":@"0",
                           @"CreateTime":@"2016-04-12T15:47:20.6928426+08:00",
                           @"Remark":@"iOS客户端测试"
                           };

    
    RegiseterTwoViewController *a = [[RegiseterTwoViewController alloc] initWithNibName:@"RegiseterTwoViewController" bundle:[NSBundle mainBundle]];
//    NSMutableDictionary *diction = [NSMutableDictionary dictionaryWithDictionary:self.dic];
    a.dic =self.dic;
    a.type  = @"注册";
    a.idcard = self.idcard.text;
    [self.navigationController pushViewController:a animated:YES];

}

- (IBAction)pushBtn:(id)sender {
   
}




-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 101|| textField.tag == 100) {
        CGRect frame = textField.frame;
        int offset = frame.origin.y + 100 - (self.view.frame.size.height - 216.0);//键盘高度216
        
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        
        [UIView commitAnimations];
        
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    self.view.frame =CGRectMake(0, 64, width, height);
    //    NSLog(@"%@", textField.text);
}

//判断手机号码
- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,183,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189
     */
    NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[0235-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188, 183
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186 145
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189  181
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
    /**
     25         * 大陆地区固话及小灵通
     26         * 区号：010,020,021,022,023,024,025,027,028,029
     27         * 号码：七位或八位
     28         */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

@end
